using System;
using System.IO;
using Moq;
using NUnit.Framework;
using SpecMaker.Core;
using SpecMaker.Core.Interfaces;

namespace SpecMaker.UnitTests
{
    [TestFixture]
    public class SpecCommandLineClientWithZeroArgs: BaseContextWhenReadingArgs
    {
        [SetUp]
        public override void SetUp()
        {
           base.SetUp();
            _client.Run(new string[] {});

        }
        [Test]
        public void should_display_help_message()
        {
           
            _wrapper.Verify(x=>x.WriteLine("usage: specmaker.exe <Assembly Location> -report <HTML|XML|CONSOLE>"));
        }

        [Test]
        public void should_display_report_explanation()
        {
            _wrapper.Verify(x=>x.WriteLine("-report is optional and defaults to a CONSOLE report"));

        }
    }
    [TestFixture]
    public class SpecCommandLineClientWithAtLeastOneArg : BaseContextWhenReadingArgs
    {
        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            _client.Run(new string[] { "assembly" });

        }

        [Test]
        [Ignore("need to rethink structure of application")]
        public void should_run_spec_runner_on_assembly()
        {
            var specrunnerloc = Path.GetDirectoryName((typeof (SpecRunner).Assembly.Location));
            _specrunner.Verify(x=>x.Execute(specrunnerloc+"\\assembly"));
        }
    }
    [TestFixture]
    public class SpecCommandLineClientWhenPassingReportOfHTML : BaseContextWhenReadingArgs
    {
        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            _client.Run(new string[] { "assembly", "-report", "HTML" });

        }
        [Test]
        public void should_load_spec_runner_with_html_reporter()
        {

            _appconfig.Verify(x => x.InitializeRunner(typeof(HtmlReporter)));
        }
    }
    [TestFixture]
    public class SpecCommandLineClientWhenPassingReportOfConsole : BaseContextWhenReadingArgs
    {
        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            _client.Run(new string[]{"assembly","-report","CONSOLE"});

        }

        [Test]
        public void should_load_spec_runner_with_console_reporter()
        {

            _appconfig.Verify(x=>x.InitializeRunner(typeof(ConsoleReportFactory)));
        }
    }
    [TestFixture]
    public class SpecCommandLineClientWhenPassingReportOfConsoleInLowerCase : BaseContextWhenReadingArgs
    {
        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            _client.Run(new string[] { "assembly", "-report","console" });

        }
        [Test]
        public void should_load_spec_runner_with_console_reporter()
        {

            _appconfig.Verify(x => x.InitializeRunner(typeof(ConsoleReportFactory)));
        }
    }
    public class BaseContextWhenReadingArgs
    {
        protected CommandLineClient _client;
        protected Mock<IOutputWrapper> _wrapper;
        protected Mock<WindsorAppConfig> _appconfig;
        protected Mock<ISpecRunner> _specrunner;
        [SetUp]
        public virtual void SetUp()
        {
            _specrunner = new Mock<ISpecRunner>();
            _appconfig = new Mock<WindsorAppConfig>();
            _appconfig.Setup(x => x.InitializeRunner(It.IsAny<Type>())).Returns(_specrunner.Object);
            _wrapper = new Mock<IOutputWrapper>();
            _client = new CommandLineClient(_wrapper.Object, _appconfig.Object);
            

        }
    }
}